package com.example.sentineldemo.config;

import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import java.util.List;

/**
 * @Package: com.example.sentineldemo.config
 * @ClassName: DataSourceInitFunc
 * @Author: Think
 * @CreateTime: 2021/7/25 15:48
 * @Description:
 */
public class DataSourceInitFunc implements InitFunc {
    @Override
    public void init() throws Exception {
        String remoteAddress = "192.168.31.17:8848";
        String groupId = "DEFAULT_GROUP";
        String dataId = "sentinel-nacos";

        ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new NacosDataSource<>(remoteAddress, groupId, dataId,
                source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {}));
        FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
    }
}
